Learn T-SQL Commands with Samples
Skip Navigation Links
Skip Navigation Links.
Expand DatabaseDatabase
Expand TableTable
Expand ViewView
Expand Stored ProcedureStored Procedure
Expand Data FilteringData Filtering
Expand Data GroupingData Grouping
Collapse JoinsJoins
Expand TriggerTrigger
Expand CursorCursor
Expand OperatorsOperators
Expand ConstraintsConstraints
Expand FunctionsFunctions
Expand Conditional ProcessingConditional Processing
Expand LoopingLooping
Expand Error HandlingError Handling
Expand v.IMP Queriesv.IMP Queries
Expand XMLXML
Expand Query PerformanceQuery Performance
Expand QueriesQueries
Expand NormalizationNormalization
Expand CreateCreate
     

Self Join

 
---- A table can be joined to itself in a self-join
tblWorld
CountryID Name ContinentID
1 Vijay 3
2 USA 4
3 Asia NULL
4 N.America NULL
5 Nepal 3
6 Canada 4
SELECT c1.name AS Continent, c2.Name Country FROM tblWorld c1 JOIN tblWorld c2 ON c1.CountryID = c2.ContinentID
Output
Continent Country
Asia India
Asia Nepal
N.America USA
N.America Canada